package com.sdleyou.treepro.common.rabbitmq.receiver;

import com.sdleyou.treepro.common.rabbitmq.AmqpExchange;
import com.sdleyou.treepro.common.rabbitmq.event.StepChangeEvent;
import com.sdleyou.treepro.common.rabbitmq.event.TreeChangeEvent;
import com.sdleyou.treepro.common.rabbitmq.message.StepChangeMsg;
import com.sdleyou.treepro.common.rabbitmq.message.TreeChangeMsg;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商品变化消费者
 *
 * @author fk
 * @version v2.0
 * @since v7.0.0 2018年3月23日 上午10:29:54
 */
@Component
public class TreeChangeReceiver {

    protected final Log logger = LogFactory.getLog(this.getClass());

    @Autowired(required = false)
    private List<TreeChangeEvent> events;

    /**
     * 步数变化
     *
     * @param treeChangeMsg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = AmqpExchange.TOTAL_TREE_CHANGE + "_QUEUE"),
            exchange = @Exchange(value = AmqpExchange.TOTAL_TREE_CHANGE, type = ExchangeTypes.FANOUT)
    ))
    public void treeChange(TreeChangeMsg treeChangeMsg) {

        if (events != null) {
            for (TreeChangeEvent event : events) {
                try {
                    event.treeChange(treeChangeMsg);
                } catch (Exception e) {
                    logger.error("处理步数变化消息出错", e);
                }
            }
        }

    }
}
